<?php 
/**
 * 订单表模型
 * @author 杨帆 
 */
class OrderBillModel extends Model{
    //表明
    public $table = 'order_bill';
    
    /**
     * 提交订单
     */
    public function _addOrder(){
        if(!$this->create()) return false;
        //订单号
        $OrderId =Cart::getOrderId();
//      p($OrderId);die;
        //订单总价
        $totalPrice = Cart::getTotalPrice();
        //获取商品信息
        $CartData = Cart::getGoods();
//      p($CartData);
        foreach ($CartData as $k => $v) {
            $CartData[$k]['options'] = implode(',', $v['options']);
            $sid[$k] = $k;
        }
        //p($sid);
//      p($CartData);die;
        
        //通过隐藏域  获取input里面的value值，也就是地址ID
        $adid =Q('post.check');
        $addRessData = K('Address')->where("addid={$adid}")->find();
//      p($addRessData);die;

        //订单表
        $data = array(
            'number'    =>$OrderId,//订单编号
            'consignee' =>$addRessData['consignee'],//收货人
            'address'   =>$addRessData['address'],//地址
            'mobile'    =>$addRessData['mobile'],//联系方式
             'amount'   =>$totalPrice,//订单总金额
               'time'   =>time(),//订单生成时间
             'status'   =>0,//订单状态
           'user_uid'   =>$_SESSION['uid'],//用户
              'delid'   =>Q('post.delid',0,'intval'),//配送方式及时间
              'payid'   =>Q('post.payid',0,'intval'),//支付方式
              
            
        );
        //返回自增oid
        $oid = $this->add($data);
        
        //定义空数组
        $listdata = array();
        foreach ( $sid as $v) {
            $listData[$v] = $CartData[$v];
        }
        
        //订单详细表
        foreach ($listData as $v){
            $DATA = array(
                'fqty'           =>$v['num'],//数量
                'pinal_price'    =>$v['price'],//最终单价
                'amount'         =>$v['total'],//金额小计
                'goods_gid'      =>$v['id'],//商品id
                'order_bill_oid' =>$oid,//订单主表id
            );
            K('OrderList')->add($DATA);
        }
        
        return $oid;
    }

    /**
     * 修改订单状态
     */
    public function editStatus($oid,$num){
        //获取订单数据
        $data = $this->where("oid={$oid}")->find();
        if($num-$data['status']==1){
            //改变数据的单据状态；
            $data['status'] =  $num; 
            //修改数据
            $this->where("oid={$oid}")->update($data);
            return true;    
        }
        return false;
    }
    
    
    
    
 }
 
 
 
 
 
 
 
 
 
 
 

 ?>